import * as Service from './service';

export default {
  namespace: 'docker/network',
  state: {
    networkList: [],
    selectKeys: [],
    visibleCreate:false
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/docker/network') {
          dispatch({ type: 'query' });
        }
      });
    },
  },
  effects: {
    * query({ payload }, { call, put }) {
      const { data } = yield call(Service.getList);
      yield put({ type: 'update', payload: { networkList: data.data } });
    },
    * updateSelect({ payload }, { call, put }) {
      yield put({ type: 'update', payload: { selectKeys: payload.select } });
    },
    * remove({ payload }, { call, put }) {
      const { data } = yield call(Service.remove, payload);
      if (data.code === 200) {
        yield put({type:'update',payload:{selectKeys:[]}});
        yield put({ type: 'query' });
      }
    },
    * create({ payload }, { call, put }) {
      const { data } = yield call(Service.create, payload.data);
      if (data.code === 200) {
        yield put({ type: 'query' });
      }
    },
    * visibleCreateState({ payload }, { call, put }) {
      yield put({type:'update',payload:{visibleCreate: payload.visible}})
    },
  },
  reducers: {
    update(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
